package com.sphd.Restaurant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sphd.Restaurant.R;
import com.sphd.Restaurant.base.BaseActivity;
import com.sphd.Restaurant.infos.IndentInfo;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.InOrOutOrdersP;
import com.sphd.Restaurant.utils.AppManager;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.InOrOutOrdersView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class IndentDetailsActivity extends BaseActivity implements InOrOutOrdersView {
    Intent intent;
    Bundle bundle;
    IndentInfo indentInfo;
    @BindView(R.id.into_cg)
    ImageView intoCg;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_orderSate)
    TextView tvOrderSate;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_srf)
    SwipeRefreshLayout mySrf;
    View headView, feetView;
    TextView tvOrderNo, tvOrderTime, tvCancleCause;
    TextView tvHeji;
    LinearLayout layoutCancle;

    TextView tvAllorderReal;
    TextView tvOrderDicout;
    TextView tvOrderReal;

    TextView tvCardpay;
    TextView tvMoneypay;
    TextView tvAddress;
    TextView tvName;
    TextView tvPhone;
    LinearLayout addressLayout;
    InOrOutOrdersP inOrOutOrdersP;
    List<IndentInfo> infoList;
    BaseQuickAdapter<IndentInfo> baseQuickAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_indent_details);
        AppManager.getAppManager().addActivity(this);

        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        infoList = new ArrayList<>();
        setRefresh(mySrf);
        setRecy(myRecy);
        indentInfo = (IndentInfo) bundle.getSerializable(Constants.INDENT);
        headView = View.inflate(context, R.layout.indent_details_head, null);

        tvOrderNo = getView(R.id.tv_orderNo, headView);
        tvOrderTime = getView(R.id.tv_orderTime, headView);
        tvCancleCause = getView(R.id.tv_cancleCause, headView);
        layoutCancle = getView(R.id.layout_cancle, headView);

        feetView = View.inflate(context, R.layout.indent_details_feet, null);
        tvHeji = getView(R.id.tv_heji, feetView);
        tvAllorderReal = getView(R.id.tv_allorderReal, feetView);
        tvOrderDicout = getView(R.id.tv_orderDicout, feetView);
        tvOrderReal = getView(R.id.tv_orderReal, feetView);
        tvCardpay = getView(R.id.tv_Cardpay, feetView);
        tvMoneypay = getView(R.id.tv_Moneypay, feetView);
        tvAddress = getView(R.id.tv_address, feetView);
        tvName = getView(R.id.tv_name, feetView);
        tvPhone = getView(R.id.tv_phone, feetView);
        addressLayout = getView(R.id.address_layout, feetView);

        baseQuickAdapter = new BaseQuickAdapter<IndentInfo>(R.layout.indent_details_item, infoList) {
            @Override
            protected void convert(BaseViewHolder helper, IndentInfo item) {
                helper.setText(R.id.tv_details_name, item.getDetail_dname())
                        .setText(R.id.tv_details_num, String.valueOf(item.getDetail_dnumber()))
                        .setText(R.id.tv_details_price, turn(item.getDetail_price(), 1).toString())
                        .setText(R.id.tv_details_vipprice, turn(item.getDetail_vipprice(), 1).toString());
            }
        };
        baseQuickAdapter.addHeaderView(headView);
        baseQuickAdapter.addFooterView(feetView);
        myRecy.setAdapter(baseQuickAdapter);

        initTitle();
        initEvent();
    }

    private void initEvent() {
        mySrf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                addDate();
            }
        });
        toolbar.setNavigationOnClickListener(view -> AppManager.getAppManager().finishActivity(this));
    }

    private void addDate() {
        inOrOutOrdersP.addData(Model.HTTPURL + Model.GETINDENTDETAILS + "orderno=" + indentInfo.getOrder_no());
    }

    private void initTitle() {
        if (bundle.getInt(Constants.TYPE) == 0) {
            addressLayout.setVisibility(View.GONE);
            tvHeji.setText("商品合计");
        } else {
            tvAddress.setText(indentInfo.getOrder_addr());
            tvName.setText(indentInfo.getOrder_recName());
            tvPhone.setText(indentInfo.getOrder_uphone());
        }
        setToolBar(toolbar, indentInfo.getOrder_cg(), true);
        tvOrderNo.setText(indentInfo.getOrder_no());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        tvOrderTime.setText(format.format(new Date(indentInfo.getOrder_fintime())));

        tvOrderDicout.setText("-￥" + turn(indentInfo.getOrder_ticket(), 1));
        tvOrderReal.setText("￥" + turn(indentInfo.getOrder_price(), 1));

        tvAllorderReal.setText("￥" + turn(indentInfo.getOrder_price() + indentInfo.getOrder_ticket(), 1));
        tvCardpay.setText("￥" + indentInfo.getOrder_cardpay());
        tvMoneypay.setText("￥" + indentInfo.getOrder_lastprice());

        switch (indentInfo.getOrder_status()) {
            case 0:
                tvOrderSate.setText("未接收");
                break;
            case 1:
                tvOrderSate.setText("已接收");
                break;
            case 2:
                tvOrderSate.setText("派送中");
                break;
            case 3:
                tvOrderSate.setText("已完成");
                tvOrderSate.setTextColor(getResources().getColor(R.color.mycolor));
                break;
            case 6:
                tvOrderSate.setText("已取消");
                layoutCancle.setVisibility(View.VISIBLE);
                tvCancleCause.setText(indentInfo.getOrder_remark());
                break;
        }
        inOrOutOrdersP = new InOrOutOrdersP(this);
        addDate();
    }


    public <T extends View> T getView(int viewId, View view) {
        return (T) view.findViewById(viewId);
    }

    @OnClick(R.id.into_cg)
    public void onClick() {
        bundle.putString(Constants.TAG, CoupinsDetailsActivity.class.getName());
        bundle.putString(Constants.CG_ID, String.valueOf(indentInfo.getOrder_cgid()));
        startActivity(RestaurantPageActivity.class, bundle);
    }

    @Override
    public void sendResList(List<IndentInfo> infos) {
        baseQuickAdapter.setNewData(infos);

    }

    @Override
    public void showdialog() {
        showDialog();
    }

    @Override
    public void dissmissdialog() {
        dissMissDialog();
        mySrf.setRefreshing(false);
    }
}
